package com.airbnb.mvrx.sample.features.book.views

import android.content.Context
import android.widget.FrameLayout
import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView
import com.airbnb.epoxy.TypedEpoxyController
import com.airbnb.mvrx.sample.Utils
import com.airbnb.mvrx.sample.core.UpApplication
import com.airbnb.mvrx.sample.features.book.models.FuncGrid
import com.airbnb.mvrx.sample.features.book.models.PriceModel
import com.example.mvrxstudy.R

@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT)
class BookDetailFuncList @JvmOverloads constructor(context: Context) : FrameLayout(context) {

    private val mRvList: GridEpoxyRecyclerView
    private val mController: FuncListController

    init {
        inflate(context, R.layout.view_func_list_book_detail, this)
        mRvList = findViewById(R.id.rv_list)
        mController = FuncListController()
        mRvList.setController(mController)
    }

    @ModelProp
    fun setList(list: List<FuncGrid>) {
        mController.setData(list)
    }

    class FuncListController : TypedEpoxyController<List<FuncGrid>>() {

        override fun buildModels(data: List<FuncGrid>) {
            val width = Utils.getDM(UpApplication.getApp()).widthPixels / data.size
            data.forEach { item ->
                bookDetailFuncGridItem {
                    id(item.title)
                    image(item.icon)
                    title(item.title)
                    width(width)
                    price(PriceModel(item.authType, item.authVal))
                }
            }
        }

    }

}
